DISTANCES = Soergel Canberra Sharing
DISTANCE_FILES = $(DISTANCES:%=ALL.%.res)
MULTI_DIM_FILES = $(DISTANCES:%=%.mds)
MDS1= java -jar mdsj.jar -s-1
MDS = java -jar mdsj.jar -s-1

NULL =  

NAME = isotype
NAMEMIN = iso_min
OPTIONS = -V --ignore-module J
GG = gg
FREQ = freq
THRESHOLD = ts
EXEC = $(NAME).opt
MINAUX = $(NAMEMIN)

SOURCES = \
  optionMonad.ml \
  stringAux.ml \
  funAux.ml \
  listAux.ml \
  distance.ml \
  flags.ml \
  ast.ml \
  pp.ml \
  resolver.ml \
  type.ml \
  equivalence.ml \
  parser.ml \
  lexer.ml \
  main.ml \
  $(NULL)

OBJS = $(SOURCES:%.ml=%.cmo)

$(NAME): $(OBJS)
	ocamlc -o $@ unix.cma $^

opt: $(NAME).opt

$(NAME).opt: $(OBJS:%.cmo=%.cmx)
	ocamlopt -o $@ unix.cmxa $^

lexer.cmo : parser.cmi
parser.cmo parser.cmx : parser.cmi

%.cmi : %.mli
	ocamlc -c $<

%.cmo %.cmi : %.ml
	ocamlc -c $<

%.cmx %.cmi : %.ml
	ocamlopt -c $<

%.ml : %.mll
	ocamllex $<

%.ml %.mli : %.mly
	ocamlyacc -v $<

dep:
	ocamldep *.ml *.mli >.depend

include .depend

$(GG): gg.hs
	ghc -o $@ $^

$(FREQ): frequency.hs
	ghc -o $@ $^

$(THRESHOLD): threshold.hs
	ghc -o $@ $^

.PHONY: clean dep check test plot 
clean:
	rm -f $(NAME) $(NAME).opt parser.mli *.o parser.output *.cmo *.cmi *.cmx

clean_dist:
	rm -f $(DISTANCE_FILES)

%.res.focus: $(EXEC)
ifeq ($(PROG),)
		@echo "Syntax make DISTANCE.plot.focus PROG=\"PROG_NUMBER\""
else
		./$(EXEC) $(OPTIONS) --distance $(@:%.res.focus=%) --focus $(PROG) $(TESTS) >$@
endif

%.dist: %.res
	tr -s ' ' <$< | cut -d ' ' -f2 >$@

A.moss.res: ALL.moss.res 
	cat $< | ./extractor.pl -filter $(PROJECT_A) >$@

B.moss.res: ALL.moss.res 
	cat $< | ./extractor.pl -filter $(PROJECT_B) >$@

C.moss.res: ALL.moss.res 
	cat $< | ./extractor.pl -filter $(PROJECT_C) >$@

D.moss.res: ALL.moss.res 
	cat $< | ./extractor.pl -filter $(PROJECT_D) >$@

A.jplag.res: ALL.jplag.res 
	cat $< | ./extractor.pl -filter $(PROJECT_A) >$@

B.jplag.res: ALL.jplag.res 
	cat $< | ./extractor.pl -filter $(PROJECT_B) >$@

C.jplag.res: ALL.jplag.res 
	cat $< | ./extractor.pl -filter $(PROJECT_C) >$@

D.jplag.res: ALL.jplag.res 
	cat $< | ./extractor.pl -filter $(PROJECT_D) >$@

%.pdist: %.dist
	./$(THRESHOLD) 0.5 0.5 15 <$< >$@

%.freq: %.dist $(FREQ)
	sort -n $< | ./$(FREQ) >$@

%.mat.xml: %.dist $(GG)
	./$(GG) <$< >$@

%.mat.txt: %.dist $(GG)
	./$(GG) -t <$< >$@

%.mds_names: %.mat.txt names
	$(MDS) $< tmp
	paste tmp names >$@

%.mds: %.mat.txt
	$(MDS) $< $@

%.global_scatter.gp: global_scatter.template
	sed s/DIST/$(@:%.global_scatter.gp=%)/g <$< >$@

%.scatter.gp: scatter.template
	sed s/DIST/$(@:%.scatter.gp=%)/g <$< >$@

%.distribution.gp: distribution.template
	sed s/DIST/$(@:%.distribution.gp=%)/g <$< >$@

%.frequency.gp: frequency.template
	sed s/DIST/$(@:%.frequency.gp=%)/g <$< >$@

%.combo_frequency.gp: combo_frequency.template
	sed s/PROG/$(@:%.combo_frequency.gp=%)/g <$< >$@

%.global_scatter.plot: %.global_scatter.gp %.mds_names
	gnuplot -persist $<

%.scatter.plot: %.scatter.gp %.mds_names
	gnuplot -persist $<

%.distribution.plot: %.distribution.gp %.dist
	gnuplot -persist $<

%.frequency.plot: %.frequency.gp %.freq
	gnuplot -persist $<

%.combo_frequency.plot: %.combo_frequency.gp %.combo_freq
	gnuplot -persist $<

%.combo_freq: %.Soergel.freq %.Sharing.freq %.moss.freq %.jplag.freq
	paste $^ >$@

%.diff.gp: graph.template 
	sed s/DIST/$(@:%.diff.gp=%)/g $< >$@

%.plot.focus: %.diff.gp %.res.focus
	gnuplot -persist $<	

%.min.gp: min.template
	sed s/DIST/$(@:%.min.gp=%)/g $< >$@

%.min.plot: %.min.gp %.res.min
	gnuplot -persist $<

%.combo_min.gp: combo_min.template
	sed s/PROG/$(@:%.combo_min.gp=%)/g <$< >$@

%.combo_min.plot: %.combo_min.gp %.combo_min
	gnuplot -persist $<

%.combo_min_cross.plot: %.combo_min_cross.gp %.combo_min_cross
	gnuplot -persist $<

%.combo_min_cross.gp: combo_min_cross.template
	sed s/PROG/$(@:%.combo_min_cross.gp=%)/g <$< >$@

%.combo_min: %.Soergel.res.min %.Sharing.res.min %.moss.res.min %.jplag.res.min
	paste $^ >$@

names: Makefile
	echo $(PROJECT_A) $(PROJECT_B) $(PROJECT_C) $(PROJECT_D) | tr ' ' '\n' >$@

plots: $(DISTANCES:%=%.2.gp) $(DISTANCES:%=%.3.gp)

distances: $(DISTANCE_FILES)

multi_dim: $(MULTI_DIM_FILES)

dist.%: res.%
	tr -s ' ' <$< | cut -d ' ' -f3 >$@

dist.%.xml: dist.% $(GG)
	./$(GG) <$< >$@

dist.%.txt: dist.% $(GG)
	./$(GG) -t <$< >$@

dist.%.mds: dist.%.txt
	java -jar mdsj.jar -s2 $< $@

ALL.%.res: $(EXEC)
	./$(EXEC) $(OPTIONS) --distance $(@:ALL.%.res=%) $(PROJECT_A) $(PROJECT_B) $(PROJECT_C) $(PROJECT_D) >$@

A.%.res: $(EXEC)
	./$(EXEC) $(OPTIONS) --distance $(@:A.%.res=%) $(PROJECT_A) >$@

B.%.res: $(EXEC)
	./$(EXEC) $(OPTIONS) --distance $(@:B.%.res=%) $(PROJECT_B) >$@

C.%.res: $(EXEC)
	./$(EXEC) $(OPTIONS) --distance $(@:C.%.res=%) $(PROJECT_C) >$@

D.%.res: $(EXEC)
	./$(EXEC) $(OPTIONS) --distance $(@:D.%.res=%) $(PROJECT_D) >$@

ALL.combo_min_cross: ALL.Soergel.res ALL.Sharing.res ALL.moss.res ALL.jplag.res ALL.Sherlock.res
	./extractor.pl -cm $^ $(TESTS) | sort -t ' ' -k 2 | uniq  >$@

A.combo_min_cross: A.Soergel.res A.Sharing.res ALL.moss.res ALL.jplag.res ALL.Sherlock.res 
	./extractor.pl -cm $^ $(PROJECT_A) | sort -t ' ' -k 2 | uniq >$@

B.combo_min_cross: B.Soergel.res B.Sharing.res ALL.moss.res ALL.jplag.res ALL.Sherlock.res
	./extractor.pl -cm $^ $(PROJECT_B) | sort -t ' ' -k 2 | uniq >$@

C.combo_min_cross: C.Soergel.res C.Sharing.res ALL.moss.res ALL.jplag.res ALL.Sherlock.res
	./extractor.pl -cm $^ $(PROJECT_C) | sort -t ' ' -k 2 | uniq >$@

D.combo_min_cross: D.Soergel.res D.Sharing.res ALL.moss.res ALL.jplag.res ALL.Sherlock.res
	./extractor.pl -cm $^ $(PROJECT_D) | sort -t ' ' -k 2 | uniq >$@

ALL.%.res.min: ALL.%.res
	cat $<  | ./extractor.pl -min $(TESTS) >$@

A.%.res.min: ALL.%.res
	cat $<  | ./extractor.pl -min $(PROJECT_A) >$@

B.%.res.min: ALL.%.res
	cat $<  | ./extractor.pl -min $(PROJECT_B) >$@

C.%.res.min: ALL.%.res
	cat $<  | ./extractor.pl -min $(PROJECT_C) >$@

D.%.res.min: ALL.%.res
	cat $<  | ./extractor.pl -min $(PROJECT_D) >$@

%.focus:
	./$(EXEC) --focus $(@:%.focus=%) $(PROJECT_A)

# LL1 PARSER
PROJECT_A = 0 2 7 9 14 15 16 18 21 23 24 25 26 30 31 32 34 35 37 38 39 40 45 46 49 51 54 58 59 64 81 82 83 84 85 87 88 91 92 93 94 96 97 98 100 101 103 105 106 107 108 109 110 111 112 114 115

# Regular EXPRESSIONS
PROJECT_B = 1 3 4 5 6 8 10 11 12 13 17 19 22 27 28 29 33 36 42 43 48 50 52 53 55 63 66 78 79 80 86 89 99 102 104 113 116

# While++ COMPILER
PROJECT_C = 20 41 44 47 56 60 61 68 69 70 71 72 74 75 76 77 90

# C-- COMPILER
PROJECT_D = 57 62 65 67 73

.PHONY: numbers
numbers:
	@echo $(PROJECT_A) | wc -w | cut -d ' ' -f2
	@echo $(PROJECT_B) | wc -w | cut -d ' ' -f2
	@echo $(PROJECT_C) | wc -w | cut -d ' ' -f2
	@echo $(PROJECT_D) | wc -w | cut -d ' ' -f2

.PHONY: pippo

pippo:
	@echo $(TEST)

TEST = \
  "cat projects"

TESTS = \
  $(PROJECT_A) \
  $(PROJECT_B) \
  $(PROJECT_C) \
  $(PROJECT_D) \
  $(NULL)

UNUSED_TESTS = \
  $(NULL)

